/**
 * Copyright 2006 gworks.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 */
package org.javaongems.client.stdctrls;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.javaongems.client.rt.text.dom.DomExtUtils;
import org.javaongems.client.rt.text.dom.UiObjectExtUtils;


import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

/**
 * <p>Provides a group panel whose content is intended to be organised as
 * label/field pairs.</p>
 * 
 * @author Ashin Wimalajeewa (ash)
 */
public class FieldsetLayoutPanel extends Panel {
	public interface Styler {
		public void styleLayout(int row, int col, Element tdHost, Widget widget);
	}
	
	protected Element container;
	protected Element fieldset;
	protected Element legend;
	protected Element table;
	protected Element tbody;
	protected String groupBoxTitle;
	protected int columns = 2;
	protected List widgets = new ArrayList();
	protected Styler styler = new TopRightFirstStyler();
	
	public FieldsetLayoutPanel() {
		container = DOM.createDiv();
	    setElement(container);
		DOM.setIntStyleAttribute(container, "padding", 6);
	    fieldset = DomExtUtils.createAndAppendElement(container, "fieldset");
	    DOM.setStyleAttribute(fieldset, "padding", "4px");
	    setTitle("");
	    table = DomExtUtils.createAndAppendElement(fieldset, "table");
	    DOM.setStyleAttribute(table, "width", "100%");
//	    DOM.setStyleAttribute(table, "padding", "4px");
	    UiObjectExtUtils.setStyleName(container, "gems-FieldsetLayoutPanel", true);
	}
	
	public FieldsetLayoutPanel(int cols, String title) {
		this();
		setColumnCount(cols);
		setTitle(title);
	}
	
	public void setColumnCount(int count) {
		columns = count;
	}
	
	public int getColumnCount() {
		return columns;
	}

	public void setTitle(String title) {
		groupBoxTitle = title;
		if (legend == null)
			legend = DomExtUtils.createAndAppendElement(fieldset, "legend");
		DOM.setInnerHTML(legend, title);
	}
	
	public String getTitle() {
		return groupBoxTitle;
	}
	
	public void addWidget(String label, Widget widget) {
		insertWidget(widgets.size(), label, widget);
	}
	
	public void insertWidget(int row, String label, Widget widget) {
		ArrayList widgetRow = new ArrayList();
		Widget lblWidget = createTextLabel(label);
		if (lblWidget != null)
			adopt(lblWidget);
		if (widget != null)
			adopt(widget);
		widgetRow.add(lblWidget);
		widgetRow.add(widget);
		widgets.add(row, widgetRow);
	}
	
	public Label createTextLabel(String txt) {
		if (txt == null)
			return null;
		return new Label(txt);
	}
	
	public List getWidgetsRow(int row) {
		return (List) widgets.get(row);
	}
	
	public void setStyler(Styler layoutStyler) {
		styler = layoutStyler;
	}
	
	public void render() {
		clearDomElements();
		int row = 0;
		for (Iterator iter = widgets.iterator(); iter.hasNext();row++) {
			Element tr = DomExtUtils.createAndAppendElement(tbody, "tr"); 
			ArrayList entries = (ArrayList) iter.next();
			int col = 0;
			for (Iterator iterator = entries.iterator(); iterator.hasNext();col++) {
				Widget widget = (Widget) iterator.next();
				Element td = DomExtUtils.createAndAppendElement(tr, "td");
				DOM.appendChild(td, widget.getElement());
				if (styler !=  null)
					styler.styleLayout(row, col, td, widget);
			}
			for (;col < getColumnCount(); col++) {
				Element td = DomExtUtils.createAndAppendElement(tr, "td");
				if (styler !=  null)
					styler.styleLayout(row, col, td, null);

			}
		}
	}
	
	// overrides
	public boolean add(Widget w) {
		return false;
	}
	  
	public boolean remove(Widget w) {
		return false;
	}

	public void clear() {
		clearWidgets();
		clearDomElements();
	}
	  
	public Iterator iterator() {
		ArrayList lst = new ArrayList();
		for (Iterator iter = widgets.iterator(); iter.hasNext();) {
			ArrayList entries = (ArrayList) iter.next();
			for (Iterator iterator = entries.iterator(); iterator.hasNext();) {
				Widget widget = (Widget) iterator.next();
				lst.add(widget);
			}
		}
		return lst.iterator();
	}
	
	private void clearWidgets() {
		for (Iterator iter = widgets.iterator(); iter.hasNext();) {
			ArrayList entries = (ArrayList) iter.next();
			for (Iterator iterator = entries.iterator(); iterator.hasNext();) {
				Widget widget = (Widget) iterator.next();
				if (widget != null)
					disown(widget);
			}
			entries.clear();
		}
		widgets.clear();
	}
	
	private void clearDomElements() {
		if (tbody != null)
			DOM.removeChild(table, tbody);
		tbody = DomExtUtils.createAndAppendElement(table, "tbody");
	}
	
	public class TopRightFirstStyler implements Styler {
		public void styleLayout(int row, int col, Element tdHost, Widget widget) {
			if (col == 0) 
				DOM.setStyleAttribute(tdHost, "text-align", "right");
//				DOM.setAttribute(tdHost, "align", "right");
			List widgetsRow = (List) widgets.get(row);
			int colCount = widgetsRow.size();
//			DOM.setAttribute(tdHost, "valign", "top");
			DOM.setStyleAttribute(tdHost, "vertical-align", "top");
			if (columns != colCount && col == colCount-1) {
				int diff = columns - col;
				DOM.setIntAttribute(tdHost, "colspan", diff);
			}
		}
	}
}
